package com.store.controller;

import com.store.pojo.Category;
import com.store.pojo.Product;
import com.store.service.AdminService;
import com.store.service.CategoryService;
import com.store.service.ProductService;
import com.store.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * 商品模块controller
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;

    @RequestMapping("/list")
    public String list(Model model){
        List<Product> list = productService.findAllProductInfo();
        model.addAttribute("productList",list);
        return "adminpage/product-list";
    }


    @RequestMapping("/productAddUI")
    public String productAddUI(Model model){
        List<Category>  list = categoryService.findAllCategoryInfo();
            model.addAttribute("category",list);
        return "adminpage/product-add";
    }

    //添加订单
    @RequestMapping("/addProduct")
    public String addProduct(Product product,Model model) throws ParseException {



        //使用业务层对象调用业务层方法
        int row = productService.addProduct(product);
        if (row<=0) {//失败
            //停留在添加页面绑定数据回到页面
            model.addAttribute("msg","添加失败");
            return "redirect:/productAddUI";
        }
        return "redirect:/product/list";//重定向跳到findAll
    }




    @RequestMapping("/deleteByPId")
    public String deleteByPId(int id, HttpServletResponse response) throws IOException {
        return productService.deleteByPId(id,response);
    }


    //上一页
    @RequestMapping("/toLastPage")
    //RequestParam_用于请求参数
    private String toLastPage(@RequestParam(name="currentPage",defaultValue="1") Integer currentPage , Model model){
        //当前页
        currentPage = currentPage-=1;
        Integer pageSize=10;
        //获取中记录数
        int totalSize = productService.getCountTotal();
        Integer totalPage = totalSize%pageSize!=0? totalSize/pageSize-1:totalSize/pageSize;
        if(currentPage>=totalPage){
            currentPage=totalPage;
        }
        if(currentPage<=1){
            currentPage=1;
        }
        //偏移量 每页显示的条数
        Integer offSet=(currentPage-1)*pageSize;
        List<Product> list = productService.findProductByPage(offSet, pageSize);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("productList",list);
        //当前页
        model.addAttribute("currentPage",currentPage);
        return "adminpage/product-list";
    }


    //下一页
    @RequestMapping("/toLimitPage")
    //RequestParam_用于请求参数
    private String toLimitPage(@RequestParam(name="currentPage",defaultValue="1") Integer currentPage ,Model model){
        //当前页
        currentPage = currentPage+=1;
        Integer pageSize=10;
        //获取中记录数
        int totalSize = productService.getCountTotal();
        Integer totalPage = totalSize%pageSize!=0? totalSize/pageSize+1:totalSize/pageSize;
        if(currentPage>=totalPage){
            currentPage=totalPage;
        }
        if(currentPage<=1){
            currentPage=1;
        }
        //偏移量 每页显示的条数
        Integer offSet=(currentPage-1)*pageSize;
        List<Product> list = productService.findProductByPage(offSet, pageSize);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("productList",list);
        //当前页
        model.addAttribute("currentPage",currentPage);
        return "adminpage/product-list";

    }

    @PostMapping("/upload")
    @ResponseBody
    public Map<String,Object> upload(MultipartFile file, HttpSession session){
        Map<String,Object> map = productService.doUpdateproPic(file, session);
        return map;
    }

}
